import re

ques_words=['what','where','why','when','who','whom','which','how','name']

ans_point_rules={"who":"PERSON",
"whom":"PERSON",
"when":"DATE",
"where":"GPE",
"what place":"GPE",
"what time":"TIME",
"what day":"DAY",
"what month":"MONTH",
"which month":"MONTH",
"what age":"AGE",
"how old":"AGE",
"what brand":"PRODUCT",
"how far":"LENGTH",
"how tall":"LENGTH",
"how high":"LENGTH",
"how large":"AREA",
"how big":"AREA",
"how small":"AREA",
"how heavy":"WEIGHT",
"how rich":"MONEY",
"how often":"FREQUENCY",
"how many":"NUMBER",
"how long":"LENGTH/DURATION",
"why":"REASON"
}

what_rule="W[A-Z\$]* (VB[A-Z]? )?(CD )?(DT )?(JJ[A-Z]? )*(NN[A-Z]* )*"
where_rule="W[A-Z\$]* VB[A-Z]?"
when_rule="W[A-Z\$]* VB[A-Z]?"
why_rule="W[A-Z\$]* VB[A-Z]?"
who_rule="W[A-Z\$]* (RB[A-Z]? )?(VB[A-Z]? )|MD "
whom_rule="W[A-Z\$]* (RB[A-Z]? )?(VB[A-Z]? )|MD "
which_rule="W[A-Z\$]* (DT )?(JJ[A-Z]? )*(NN[A-Z]* )*"
how_rule="W[A-Z\$]* (JJ[A-Z]? )*(NN[A-Z]* )*|(RB[A-Z]? )((VB[A-Z]? )|MD )"
name_rule="(DT )?(JJ[A-Z]? )*(NN[A-Z]* )*"


rule_dict={"whom":whom_rule,"who":who_rule,"what":what_rule,"where":where_rule,"when":when_rule,"why":why_rule,"which":which_rule,"how":how_rule,"name":name_rule}

def question_point(tagged_list):
	ques_word=""
	ans_type="UNKNOWN"
	for tagged_word_tuples in tagged_list:
		tagged_word=tagged_word_tuples[0]
		for i in ques_words:
			if tagged_word.lower()==i:
				ques_word=i
				break
		if ques_word != "":
			break
	
	if ques_word=="" :
		return ans_type
		
	ques_tag_pattern=""
	for i in tagged_list:
		ques_tag_pattern+=i[1]
		ques_tag_pattern+=" "
	
	ques_pattern=""
	for i in tagged_list:
		ques_pattern+=i[0].lower()
		ques_pattern+=" "
		
	for i in ans_point_rules.keys():
		if ques_pattern.find(i) >= 0:
			ans_type=ans_point_rules[i]
			
	return ans_type	
	
def modify_query(orig_query):

    modify_list={}
    sub="XXXXX"
    pattern1="\s'[^\n]+'\s"
    pattern2='\s"[^\n]+"\s'
    res_pattern=pattern1+"|"+pattern2
    sub_list=re.findall(res_pattern,orig_query)

    for i in sub_list:
        tmp=re.sub(i," "+sub+" ",orig_query,1)
        modify_list.update({sub:i})
        orig_query=tmp
        sub+="X"

    return (orig_query,modify_list)

